﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using DataAccess;

public partial class Manager_ManagerCategory : System.Web.UI.Page
{
    DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Message"] != null)
        {
            lblmes.Text = Session["Message"].ToString();
            Session.Remove("Message");
        }
        else
            lblmes.Text = "";

        LoadCategory();
    }

    protected void grvCategory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;
        if (e.Row.RowIndex != grvCategory.EditIndex)
        {
            LinkButton del = (LinkButton)e.Row.Cells[3].FindControl("del");
            del.Attributes.Add("onclick", "return confirm('Delete Category?')");
        }
    }
    protected void grvCategory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int CategoryID = int.Parse(grvCategory.DataKeys[e.RowIndex].Value.ToString());
        //LoadCategory();
        CategoryDAL catedal = new CategoryDAL();
        if(catedal.Delete(CategoryID))
            Session.Add("Message", "Delete CategoryID : " + CategoryID + " successfully");
        else
            Session.Add("Message", "<font color='red'>Error : Delete CategoryID : " + CategoryID + " unsuccess</font>");
         
    }
    protected void grvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvCategory.PageIndex = e.NewPageIndex;
        LoadCategory();
    }

    protected void LoadCategory()
    {
        DbCommand cmd = GenericData.CreateCommand();
        cmd.CommandText = "SELECT * FROM Category";
        table = GenericData.ExecuteReader(cmd);
        grvCategory.DataSource = table;
        grvCategory.DataBind();
    }
}